[root@nfs01 scripts]
#!/bin/sh
read -p "Please insert the first number and the method you want to caculate:" num1
read -p "Please insert the second number and the method you want to caculate:" num2
read -p "Please insert the method you want to caculate:" \opt
usage(){
echo "Usage $0 num1 num2 operation"
exit 1
}
if_int(){
num=$1
if [ ! -z $(echo $num | sed 's/[0-9]//g') ];then
usage
exit 2
fi
}
if_int $num1
if_int $num2
caculate(){
echo "${num1}${opt}${num2}=$((${num1}${opt}${num2}))"
}
if_zero(){
if [ $1 -eq 0 ];then
echo "The dividend can't be zero,please input a number which is not equal 0"
exit 3
fi
}
case $opt in
"+")
caculate $num1 $num2 $opt
;;
"-")
caculate $num1 $num2 $opt
;;
"*")
caculate $num1 $num2 $opt
;;
"/")
if_zero $num2
result=$(echo ${num1}${opt}${num2} | bc )
echo $result
;;
"%")
caculate $num1 $num2 $opt
;;
esac